/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.swosh.stomp.ejbtest;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Implementace PersonDAO. Má za ukol CRUD operace s Person entitami.
 * @author swosh
 */
@Stateless(name = "PersonDAO")
@Remote(PersonDAORemote.class)
@Local(PersonDAO.class)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class PersonDAOBean implements PersonDAO, PersonDAORemote {

    private static final Logger log = Logger.getLogger(PersonDAOBean.class.getSimpleName());
    @PersistenceContext(unitName = "stomp")
    private EntityManager em;

    /**
     * Uloží osobu do db.
     * @param p
     */
    @Override
    public void addPerson(Person p) {
        em.persist(p);
        em.flush();
    }

    @Override
    public List<Person> getPersons() {
        
        Query q = em.createNativeQuery("SELECT id, lon, municipalty,"
                + " description, numori, name,"
                + " street, image, lat  FROM Person p ");
        List rl = q.getResultList();
        List<Person> pl = new ArrayList<Person>();
        for (Iterator it = rl.iterator(); it.hasNext();) {
            Object[] object = (Object[]) it.next();
            Person p = new Person();
            p.setId((Integer) object[0]);
            p.setLon((Double) object[1]);
            p.setMunicipalty((String) object[2]);
            p.setDescription((String) object[3]);
            p.setNumOri((Integer) object[4]);
            p.setName((String) object[5]);
            p.setStreet((String) object[6]);
            p.setImage((byte[]) object[7]);
            p.setLat((Double) object[8]);


            pl.add(p);


        }
        return pl;
    }

    @Override
    public void updatePerson(Person oldP, Person newP) {
        newP.setId(oldP.getId());
        oldP = newP;
        em.merge(oldP);
        em.flush();
    }

    /**
     * Původně pro derby byl tvar
     *
     * Query q = em.createNativeQuery("SELECT id, name, lat, lon FROM Person p ", Person.class);
    List<Person> rl = q.getResultList();
    return rl;
    Query q = em.createNativeQuery("SELECT id, name, lat, lon FROM Person p ");
    List rl = q.getResultList();
     * U postgre nemůžu použít code stejný jako pro javadb. Objeví se chyba, že pk je null.
     * @return
     */
    @Override
    public List<Person> getPersonsLatLonNameId() {
        Query q = em.createNativeQuery("SELECT id, name, lat, lon FROM Person p ");
        List rl = q.getResultList();
        List<Person> pl = new ArrayList<Person>();

        for (Iterator it = rl.iterator(); it.hasNext();) {
            Object[] object = (Object[]) it.next();
            Person p = new Person();
            int id = (Integer) object[0];
            p.setId(id);
            p.setName((String) object[1]);
            double lat = (Double) object[2];
            p.setLat(lat);
            double lon = (Double) object[3];
            p.setLon(lon);
            pl.add(p);
        }
        return pl;




    }

    @Override
    public Person getPerson(int id) {
        return em.find(Person.class, id);
    }

/**
 * Maže Person entity. Velmi bolestivé bylo zjišťování, že nemohu užít
 * jen em.remove(p2). Eclipselink si neuloží pk při vytvoření objektu.
 * Očekával bych, id bude uloženo do pk
 * K pk objektu se dostanu až poté, co ho najdu metodou find.
 * @param p
 */
    @Override
    public void deletePerson(Person p) {
        Person p2 = em.find(Person.class, p.getId());
        em.remove(p2);
        em.flush();
    }


}
